#include "ZImage.h"

namespace Zdib
{
	CImager::CImager(CContainer *root,CContainer *parent,char *filename):CContainer(parent)
	{
		m_Imgs=root->GetImgForPath(filename);
		W=m_Imgs->GetWidth();
		H=m_Imgs->GetHeight();
		SetRoot(root);
		this->m_AllDir=0;
		this->m_AllFrame=0;
		this->m_CurDir=0;
		this->m_CurFrame=0;
		SetCurFrame(0);
	}
	CImager::~CImager()
	{
	}

	CBitBmp *CImager::GetCurBmp()
	{
		return m_Imgs->GetBmpForFrame(m_CurDir,m_CurFrame);
	}

	void CImager::Draw()
	{
		CImgFile *desimg;
		if(!m_Show)
			return;
		desimg=m_Parent->GetCurImg();
		if(desimg && m_Imgs)
		{
			if(m_RefrashDrawPos)
			{
				m_DPosValue=CalDrawPosValue(m_Imgs->GetWidth(),m_Imgs->GetHeight(),ScreenX,ScreenY);
			}
			desimg->Draw(GetCurBmp(),ScreenX,ScreenY,true,&m_DPosValue);
		}
		CContainer::Draw();
	}

	bool CImager::isInMyScope(int x,int y)
	{
		int px,py;
		CBitBmp *temp;
		px=x-ScreenX;
		py=y-ScreenY;
		if(px<0 || py<0)
		{
			return false;
		}
		temp=GetMyImg();
		if(temp)
		{
			return temp->isPointInImg(px,py);
		}
		return false;
	}

	bool CImager::OnEvent(const TEvent& ev)
	{
		if(ev.event_type==ET_MouseEvent)
		{
			if(myEventReceiver!=NULL)
			{
				TEvent newev;
				newev=ev;
				newev.Mouse.Call=this;
				newev.Mouse.CallBack=NULL;
				return myEventReceiver->OnEvent(newev);
			}
			else
			{
				return m_Parent->OnEvent(ev);
			}
		}
		return false;
	}

	void CCutImager::SetSize(int w,int h)
	{
		if(w<=0 || h<=0 || w>m_Imgs->GetWidth() || h>m_Imgs->GetHeight())
			return;
		W=w;
		H=h;
	}

	void CCutImager::Draw()
	{
		CImgFile *desimg;
		if(!m_Show)
			return;
		desimg=m_Parent->GetCurImg();
		if(desimg && m_Imgs)
		{
			if(m_RefrashDrawPos)
			{
				m_DPosValue=CalDrawPosValue(W,H,ScreenX,ScreenY);
			}
			desimg->Draw(GetCurBmp(),ScreenX,ScreenY,true,&m_DPosValue);
		}
		CContainer::Draw();
	}
	bool CCutImager::isInMyScope(int x,int y)
	{
		int px,py;
		CBitBmp *temp;
		px=x-ScreenX;
		py=y-ScreenY;
		if(px<0 || py<0 || px>W || py>H)
		{
			return false;
		}
		temp=GetMyImg();
		if(temp)
		{
			return temp->isPointInImg(px,py);
		}
		return false;
	}
}